﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Bunbyx.game
{
    public class Hud
    {
        // --------------------------------------------------------------------
        //  Constants
        // --------------------------------------------------------------------

        public const string FONT_NAME = "font";
        public const string SMALL_FONT_NAME = "small";
        public const string BIG_FONT_NAME = "big";

        private Vector2 TIMER_POSITION = new Vector2(20, 20);
        private Vector2 SCORE_POSITION = new Vector2(940, 20);

        private Vector2 SALAD_IMG;
        private Vector2 CARROT_IMG;
        private Vector2 IDOL_GOLD_IMG;

        private Vector2 SALAD_COUNTER;
        private Vector2 CARROT_COUNTER;
        private Vector2 IDOL_GOLD_COUNTER;

        private Vector2 LINE_HEIGHT = new Vector2(0, 30);
        private Vector2 COUNT_OFFSET = new Vector2(40, 0);

        // --------------------------------------------------------------------
        //  Public attributes
        // --------------------------------------------------------------------

        public long Score;
        public long MaxTime;

        public int MaxSalad
        {
            get
            {
                return maxSalad;
            }
            set
            {
                maxSalad = value;
                printSalad = (maxSalad > 0);

                computeCounterPositions();
            }
        }

        public int MaxCarrot
        {
            get
            {
                return maxCarrot;
            }
            set
            {
                maxCarrot = value;
                printCarrot = (maxCarrot > 0);

                computeCounterPositions();
            }
        }

        public int MaxIdolGold
        {
            get
            {
                return maxIdolGold;
            }
            set
            {
                maxIdolGold = value;
                printIdolGold = (maxIdolGold > 0);

                computeCounterPositions();
            }
        }

        public int SaladCount;
        public int CarrotCount;
        public int IdolGoldCount;

        // --------------------------------------------------------------------
        //  Private attributes
        // --------------------------------------------------------------------

        private SpriteFont font;
        private Camera camera;

        private int maxSalad;
        private int maxCarrot;
        private int maxIdolGold;

        private bool printSalad;
        private bool printCarrot;
        private bool printIdolGold;

        private Sprite saladSprite = new Sprite(Sprite.SpriteType.Salad);
        private Sprite carrotSprite = new Sprite(Sprite.SpriteType.Carrot);
        private Sprite idolGoldSprite = new Sprite(Sprite.SpriteType.IdolGold);

        // --------------------------------------------------------------------
        //  Constructor
        // --------------------------------------------------------------------

        public Hud(Camera camera)
        {
            this.camera = camera;
            Reset();
        }

        // --------------------------------------------------------------------
        //  Public methods
        // --------------------------------------------------------------------

        public void Reset()
        {
            MaxTime = 0;
            Score = 0;

            SaladCount = 0;
            CarrotCount = 0;
            IdolGoldCount = 0;

            maxCarrot = 0;
            maxSalad = 0;
            maxIdolGold = 0;
        }

        public void LoadContent(ContentManager content)
        {
            font = content.Load<SpriteFont>(FONT_NAME);
            saladSprite.LoadContent(content);
            carrotSprite.LoadContent(content);
            idolGoldSprite.LoadContent(content);
            saladSprite.Scale = 0.35f;
            carrotSprite.Scale = 0.6f;
            idolGoldSprite.Scale = 0.5f;
        }

        public void Update(GameTime gameTime)
        {
            if (MaxTime > 0)
            {
                MaxTime -= gameTime.ElapsedGameTime.Milliseconds;

                if (MaxTime < 0)
                {
                    MaxTime = 0;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, getTime(), camera.Position + TIMER_POSITION, Color.Black);
            spriteBatch.DrawString(font, Score.ToString(), camera.Position + SCORE_POSITION, Color.Black);

            if (printSalad)
            {
                saladSprite.Position = camera.Position + SALAD_IMG;
                Color color = (SaladCount < MaxSalad) ? Color.Black : Color.Gold;

                saladSprite.Draw(spriteBatch);
                spriteBatch.DrawString(font, SaladCount.ToString() + "/" + maxSalad.ToString(), camera.Position + SALAD_COUNTER, color);
            }

            if (printCarrot)
            {
                carrotSprite.Position = camera.Position + CARROT_IMG;
                Color color = (CarrotCount < MaxCarrot) ? Color.Black : Color.Gold;

                carrotSprite.Draw(spriteBatch);
                spriteBatch.DrawString(font, CarrotCount.ToString() + "/" + maxCarrot.ToString(), camera.Position + CARROT_COUNTER, color);
            }

            if (printIdolGold)
            {
                idolGoldSprite.Position = camera.Position + IDOL_GOLD_IMG;
                Color color = (IdolGoldCount < MaxIdolGold) ? Color.Black : Color.Gold;

                idolGoldSprite.Draw(spriteBatch);
                spriteBatch.DrawString(font, IdolGoldCount.ToString() + "/" + maxIdolGold.ToString(), camera.Position + IDOL_GOLD_COUNTER, color);
            }
        }

        // --------------------------------------------------------------------
        //  Private methods
        // --------------------------------------------------------------------

        private string getTime()
        {
            // Seconds
            long s = (MaxTime / 1000) % 60;
            string sString = (s < 10) ? "0" + s : s.ToString();

            // Minutes
            long m = MaxTime / 60000;
            string mString = (m < 10) ? "0" + m : m.ToString();

            // Convert
            return mString + ":" + sString;
        }

        private void computeCounterPositions()
        {
            CARROT_IMG = new Vector2(20, 50);
            SALAD_IMG = new Vector2(20, 50);
            IDOL_GOLD_IMG = new Vector2(20, 50);

            if (printCarrot)
            {
                SALAD_IMG += LINE_HEIGHT;
                IDOL_GOLD_IMG += LINE_HEIGHT;
            }

            if (printSalad)
            {
                IDOL_GOLD_IMG += LINE_HEIGHT;
            }

            CARROT_COUNTER = CARROT_IMG + COUNT_OFFSET;
            SALAD_COUNTER = SALAD_IMG + COUNT_OFFSET;
            IDOL_GOLD_COUNTER = IDOL_GOLD_IMG + COUNT_OFFSET;
        }
    }
}
